//
//  C8DynamicClass.m
//  精通Objective-C
//
//  Created by 花强 on 2017/12/12.
//  Copyright © 2017年 花强. All rights reserved.
//

//#import <Foundation/Foundation.h>
#import <objc/message.h>
#import <objc/runtime.h>

#import "C8DynamicClass.h"

@implementation C8DynamicClass

NSString * greeting(id self, SEL _cmd){
    return @"Hello world";
}

-(void)dynamicClassCreate{
    //以动态方式创建一个类DynamicClass
    Class dynamicClass = objc_allocateClassPair([NSObject class], "DynamicClass", 0);
    
    //以动态方式添加一个方法，使用已有的方法（description）获取特征
    Method description = class_getInstanceMethod([NSObject class], @selector(description));
    const char * types = method_getTypeEncoding(description);
    class_addMethod(dynamicClass, @selector(greeting), (IMP)greeting, types);
    
    //注册这个类
    objc_registerClassPair(dynamicClass);
    
    //使用该类创建一个实例并向其中发送一条消息
    id dynaObj = [[dynamicClass alloc] init];
    NSLog(@"%@",objc_msgSend(dynaObj,NSSelectorFromString(@"greeting")));//Build Setting--> Apple LLVM 6.0 - Preprocessing--> Enable Strict Checking of objc_msgSend Calls  改为 NO
    
}

@end
